const mongoose=require('mongoose');
const Users = require('../models/users.model');

exports.create=function(req,res,next){
    const kitty = new Users(req.body);
    console.log(kitty.date.toString())
    kitty.save().then(data => {
        res.json(data);
    });
}

exports.updata=function(req,res,next){
    var id = req.params.id;
    Users.findByIdAndUpdate(id,{$set:req.body},{new:false})
    .then(data=>{
        res.json(data);
    })
}

exports.get=function(req,res,next){
    var id = req.params.id;
    Users.findById(id)
    .then(data=>{
        res.json(data);
    })
}

exports.remove=function(req,res,next){
    var id = req.params.id;
    Users.findByIdAndRemove(id)
    .then(data=>{
        res.json(data);
    })
}

exports.removes=function(req,res,next){
    var ids = req.body.ids.split(',');
    Users.remove({_id:{ $in: ids}}).then(()=>{
        res.json({"message":"删除记录成功",status:200})
    })
}

exports.list=function(req,res,next){
    var page = req.body.page?req.body.page:1;
    var rows = req.body.rows?req.body.rows:5;

    
    Users.paginate({}, { page: +page, limit: +rows }, function(err, result) {
        result.rows = result.docs;
        delete result.docs;
        res.json(result);
        // result.docs
        // result.total
        // result.limit - 10
        // result.page - 3
        // result.pages
      });
}
